#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 先创建一个菜单栏
    QMenuBar* menubar = new QMenuBar();
    this->setMenuBar(menubar);

    // 创建菜单
    QMenu* m1 = new QMenu("文件");
    QMenu* m2 = new QMenu("编辑");
    QMenu* m3 = new QMenu("视图");
    menubar->addMenu(m1);
    menubar->addMenu(m2);
    menubar->addMenu(m3);

    // 给菜单添加菜单项
    QAction* a1 = new QAction("新建");
    QAction* a2 = new QAction("打开");
    QAction* a3 = new QAction("保存");
    QAction* a4 = new QAction("另存为");
    QAction* a5 = new QAction("退出");
    m1->addAction(a1);
    m1->addAction(a2);
    m1->addAction(a3);
    m1->addAction(a4);
    m1->addAction(a5);

    // 给action添加信号槽
    connect(a1, &QAction::triggered, this, &MainWindow::handle);
    connect(a5, &QAction::triggered, this, &MainWindow::close);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handle()
{
    qDebug() << "触发新建操作";
}
